/*
小结算界面(一轮游戏结束)，整个界面的处理逻辑
*/
var confige = require("confige");
var gameData = require("gameData");
cc.Class({
    extends: cc.Component,

    properties: {
        parent:{
            default:null,
            type:cc.Node
        },

        settle_perfab:{
            default:null,
            type:cc.Prefab
        },

        isInit:false,
    },

    onLoad: function () {
    },

    onInit:function(){
        this.onShow = false;
        this.isNewType = false;

        this.overCallBack = -1;
        this.showBeginBtnCall = -1;
        if(confige.roomData.roomType == "sanKung" || confige.roomData.roomType == "zhajinhua")
            this.settleItemListNode = this.node.getChildByName("settleItemListThree");
        else
            this.settleItemListNode = this.node.getChildByName("settleItemList");
        this.settleItemList = {};
        this.settleItemPosList = {};
        for(var i=0;i<9;i++){
            this.settleItemList[i] = this.settleItemListNode.getChildByName("settleOnce"+i);
            this.settleItemPosList[i] = {x:this.settleItemList[i].x,y:this.settleItemList[i].y};
        }

        this.isInit = true;
    },

    showLayer:function(){
        if(this.isInit == false)
            this.onInit();
        this.onShow = true;
        this.node.active = true;
    },

    hideLayer:function(){
        this.onShow = false;
        this.node.active = false;
    },

    updateSettleNew:function(settleData){
        var activeList = [];
        for(var i in settleData.result)
        {
            if(settleData.result.hasOwnProperty(i) && settleData.player[i].isActive)
            {
                var niuType = settleData.result[i].type;
                activeList.push(parseInt(i));
                this.settleItemList[i].active = true;
                var newSettleS = this.settleItemList[i].getComponent("settleOnce");
                newSettleS.onInit();
                newSettleS.initNewItem();
                newSettleS.setSettleInfo(settleData.player[i].playerInfo.nickname,settleData.player[i].playerInfo.uid,settleData.player[i].playerInfo.head);
                if(settleData.betList)
                    newSettleS.setSettleScore(settleData.betList[i],settleData.curScores[i]);
                else
                    newSettleS.setSettleScore(settleData.curScores[i],settleData.curScores[i]);
                newSettleS.setSettleType(settleData.result[i].type,settleData.result[i].card.num);
                newSettleS.showCard(settleData.player[i].handCard,true);
            }
        }
        console.log("activeList=====");
        console.log(activeList);
        for(var i=0;i<activeList.length-1;i++){
            for(var j=0;j<activeList.length-1-i;j++){
                if(settleData.result[activeList[j]].type < settleData.result[activeList[j+1]].type){
                    var temp=activeList[j];
                    activeList[j]=activeList[j+1];
                    activeList[j+1]=temp;
                }
                if(settleData.result[activeList[j]].type == settleData.result[activeList[j+1]].type){
                    if(settleData.result[activeList[j]].card.num < settleData.result[activeList[j+1]].card.num)
                    {
                        var temp=activeList[j];
                        activeList[j]=activeList[j+1];
                        activeList[j+1]=temp;
                    }
                }
            }
        }
        console.log(activeList);
        for(i=0;i<activeList.length;i++)
        {
            this.settleItemList[activeList[i]].x = this.settleItemPosList[i].x;
            this.settleItemList[activeList[i]].y = this.settleItemPosList[i].y;
        }
    },

    addOneSettle:function(name, type, score, gameType,handCard,chair,newMyScore,paigowCardNum,curSitWin){     //0:nomal;1:Special;2:sanKung;3:jinHua
        if(this.isNewType)
            return;
        this.settleItemList[chair].active = true;
        var newSettleS = this.settleItemList[chair].getComponent("settleOnce");
        newSettleS.onInit();
        newSettleS.setSettle(chair,name,score,type,newMyScore,paigowCardNum,curSitWin);
        newSettleS.showCard(handCard,true);
    },
    
    show:function(win){
        this.node.active = true;
    },
    
    hide:function(){
        this.node.active = false;
    },
    
    cleanData:function(){
        for(var i=0;i<9;i++)
            this.settleItemList[i].active = false;
    },

    hideNoClick:function(){
        this.cleanData();
        this.hide();
    },
    
    btn_close_click:function(){
        window.DKTools.audioEG.playEffect("tips_btnClick");
        this.cleanData();
        this.hide();
        if(this.overCallBack != -1)
            this.overCallBack();
        pomelo.clientScene.onBtnReadyClicked();
        if(pomelo.clientScene.popBanker)
            pomelo.clientScene.popBanker.active = false;
    },


    showMyScore:function(curScore){
        var winNode = this.node.getChildByName("myScoreNode").getChildByName("win");
        var loseNode = this.node.getChildByName("myScoreNode").getChildByName("lose");
        winNode.active = false;
        loseNode.active = false;
        if(curScore >= 0)
        {
            winNode.active = true;
            winNode.getComponent("cc.Label").string = "/"+ Math.round(parseInt(curScore));
        }else{
            loseNode.active = true;
            loseNode.getComponent("cc.Label").string = Math.round(parseInt(curScore));
        }
    },
});